package com.pmcc.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 登录人信息
 */
public class AppUtils {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    private static Map<String, Object> onlineUsers = new LinkedHashMap<String, Object>();

    public static Map<String, Object> getOnlineUsers() {
        return onlineUsers;
    }

    public static void addOnlineUser(String sessionId, OnlineUser online) {
        if (!onlineUsers.containsKey(sessionId)) {
            OnlineUser onlineUser = new OnlineUser();
            /*onlineUser.setIp(online.getIp());
            onlineUser.setDepCName(online.getDepCName());
            onlineUser.setDepEName(online.getDepEName());
            onlineUser.setDepId(online.getDepId());
            onlineUser.setRoleIds(online.getRoleIds());
            onlineUser.setToken(online.getToken());
            onlineUser.setUserId(online.getUserId());
            onlineUser.setSystemCode(online.getSystemCode());
            onlineUser.setUserCName(online.getUserCName());
            onlineUser.setUserEName(online.getUserEName());
            onlineUser.setDuty(online.getDuty());*/
            onlineUsers.put(sessionId, onlineUser);
        }
    }

    public static OnlineUser getOnlineUser(String token) {
        return (OnlineUser) onlineUsers.get(token);
    }

    public static void removeOnlineUser(String token) {
        onlineUsers.remove(token);
    }

    public static String getRemortIP(HttpServletRequest request) {
        if (request.getHeader("x-forwarded-for") == null) {
            return request.getRemoteAddr();
        }
        return request.getHeader("x-forwarded-for");
    }
}
